package com.yunfei.api.v2.service;

import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.BankNoParam;
import com.yunfei.api.v2.param.CustNoParam;
import com.yunfei.api.v2.param.ReserveNoParam;
import com.yunfei.api.v2.result.BankResult;
import com.yunfei.api.v2.result.GetAccountInfo;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.HeTongSignInfoGetStatusResultStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.AccountInfoMapper;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: 查询收益账号信息
* @Author: zhengyunfei
* @Date: 2019/8/9
* @time:10:48
*/
@Service
public class HTSGetAccountInfoService {
    @Autowired
    private AccountInfoMapper getAccountInfoMapper;
    public ResultModel getAccountInfo(CustNoParam param, ReserveNoParam reserveNoParam){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        Map<String,Object> reserveNoMap=new HashMap<>();
        try{
            String cCustNo=param.getcCustNo();//客户号
            String reserveNo=reserveNoParam.getReserveNo();//预约编号
            if(StringUtil.isNullOrEmpty(cCustNo)){
                resultModel.setCode(GetAccountInfoEnum.CUSTNO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.CUSTNO_NOTNULL.getMessage());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(GetAccountInfoEnum.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            //判断客户是否填写了账号信息：
            queryMap.put("cCustNo",cCustNo);
            reserveNoMap.put("reserveNo",reserveNo);
            int count=getAccountInfoMapper.isWhiteAccountInfo(reserveNoMap);
            GetAccountInfo result=null;
            if(count>0){
                //默认读取填写的信息
                 result=getAccountInfoMapper.getDefaultWriteAccountInfo(reserveNoMap);
            }else{
                 result= (GetAccountInfo) getAccountInfoMapper.getAccountInfo(queryMap);
            }
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    public ResultModel getAccountSelect(CustNoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String cCustNo=param.getcCustNo();//客户号
            if(StringUtil.isNullOrEmpty(cCustNo)){
                resultModel.setCode(GetAccountInfoEnum.CUSTNO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.CUSTNO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("cCustNo",cCustNo);
            List<GetAccountInfo> result= ( List<GetAccountInfo>) getAccountInfoMapper.getAccountSelect(queryMap);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
    * @Description: 获取银行账号信息
    * @Author: zhengyunfei
    * @Date: 2019/8/12
    * @time:10:47
    */
    public ResultModel getBankByNo(BankNoParam param) {
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String bankNo=param.getBankNo();//银行卡号
            if(StringUtil.isNullOrEmpty(bankNo)){
                resultModel.setCode(GetAccountInfoEnum.BANK_NO_NOTNULL.getCode());
                resultModel.setMessage(GetAccountInfoEnum.BANK_NO_NOTNULL.getMessage());
                return resultModel;
            }
            if(bankNo.length()>6){
                bankNo=bankNo.substring(0,6);
            }
            queryMap.put("bankNo",bankNo);
            BankResult result= ( BankResult) getAccountInfoMapper.getBankByNo(queryMap);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
  /**
  * @Description: 可选择的银行列表
  * @Author: zhengyunfei
  * @Date: 2019/8/12
  * @time:11:27
  */
    public ResultModel getBankList() {
        ResultModel resultModel=new ResultModel();
        try{
            List<BankResult> result= ( List<BankResult>) getAccountInfoMapper.getBankList();
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
    /**
    * @Description: 是否可以编辑账号信息,如果合同状态未7，则可以编辑
    * @Author: zhengyunfei
    * @Date: 2019/8/12
    * @time:14:37
    */
    public Object isEdite(ReserveNoParam param) {
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveNo=param.getReserveNo();
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setCode(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getCode());
                resultModel.setMessage(HeTongSignInfoGetStatusResultStatus.RESERVENO_NOTNULL.getMessage());
                return resultModel;
            }
            queryMap.put("reserveNo",reserveNo);
            String result=getAccountInfoMapper.isEdite(queryMap);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }
}
